package com.sinoglobal.dumping.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.adapter.Dumpling_MakeCardAdapter;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_NetWorkUtil;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_BaseVo;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyCashDumplingBean;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyCashDumplingBean.MyCashDumpling;
import com.sinoglobal.dumping.view.Dumpling_CustomListView;
import com.sinoglobal.dumping.view.Dumpling_CustomListView.LoadData;
import com.sinoglobal.dumping.view.Dumpling_CustomListView.OnRefreshListener;

@SuppressLint("HandlerLeak")
public class Dumpling_MakeCardDumplingListActivity extends Dumpling_SinoBaseActivity implements OnItemClickListener {
	
	protected static final int LOAD_MORE_DATA_ERROR = 0;
	protected static final int LOAD_MORE_DATA_SUCCESS = 1;
	private Dumpling_CustomListView lv_my_card_dumpling_list;
	private Dumpling_MakeCardAdapter dumpling_MakeCardAdapger;
	private View  hasDataView, noDataView , loadDataErrorView , hasNetworkConnectedView ;
	private double putMoney;
	private int putMoneyNumber;
	private boolean isRefreshed = true ;  // 默认 没有开启刷新
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			
			if(msg.what == LOAD_MORE_DATA_ERROR){
				lv_my_card_dumpling_list.setHas(hasMore() ? 1 : 0);
			} else if (msg.what == LOAD_MORE_DATA_SUCCESS) {
				lv_my_card_dumpling_list.setHas(hasMore() ? 1 : 0);
				lv_my_card_dumpling_list.showFooter2(hasMore() ? false : true);
			}
		}
		
	} ;


	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		  View view = getView(R.id.title_right_btn_share);
	      hideView(mTemplateRightText, mTemplateRightImg,view);
	      mTemplateTitleText.setText("包贺卡");
	      setContentView(R.layout.dumpling_activity_make_card_dumpling);
	      
	      putMoney = getIntent().getExtras().getDouble("putMoney");
	      putMoneyNumber = getIntent().getExtras().getInt("putMoneyNumber");
	      
	      init();
	}

	private void init() {
		initView();
		
		initAdapter();
		
		initData();
	}
	
	private void initView() {
		
		hasDataView = findViewById(R.id.id_has_data);
		noDataView = findViewById(R.id.id_no_fragment);
		TextView tv_has_nodata_des = (TextView) noDataView.findViewById(R.id.tv_has_nodata_des);
		tv_has_nodata_des.setText("你还没有包贺卡了，赶紧去包吧");
		loadDataErrorView = findViewById(R.id.id_load_data_errorview);
		hasNetworkConnectedView = findViewById(R.id.id_network_has_connected);
		
		TextView tv_make_cash_dumpling_totolprice = (TextView) findViewById(R.id.tv_make_cash_dumpling_totolprice);
		TextView tv_make_cash_dumpling_totolnumber = (TextView) findViewById(R.id.tv_make_cash_dumpling_totolnumber);
		tv_make_cash_dumpling_totolprice.setText("共包"+this.putMoney + "个美图");
		tv_make_cash_dumpling_totolnumber.setText("贺卡饺子"+this.putMoneyNumber+"个");
		
		lv_my_card_dumpling_list = (Dumpling_CustomListView) findViewById(R.id.lv_my_card_dumpling_list);
		lv_my_card_dumpling_list.setOnItemClickListener(this);
	}
	
	private void initAdapter() {
		if (dumpling_MakeCardAdapger == null) {
			dumpling_MakeCardAdapger = new Dumpling_MakeCardAdapter(this);
		}
		lv_my_card_dumpling_list.setAdapter(dumpling_MakeCardAdapger);
	}
	
	private void initData() {
		
		onRefreshData(); // 第一次加载数据
		
		// 是否启用下拉刷新，由子类去重写
		if (this.isRefreshEnable()) {
			this.lv_my_card_dumpling_list.setOnRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh(Dumpling_CustomListView listView) {
					onRefreshData();
				}
			});
		}
		// 是否启用上拉加载更多，由子类重写
		if (this.isLoadMoreEnable()) {
			this.lv_my_card_dumpling_list.getMore(new LoadData() {

				@Override
				public void automLoadData() {
					onLoadMoreData();
				}

				@Override
				public boolean isHasMore() {
					return hasMore();
				}
			});
		}

				
	
	}

	/**
	 *  判断有没有更多列表数据
	 * @return
	 */
	public boolean hasMore() {
		return Dumpling_BaseVo.isLoadMoreData();
	}
	
	/**
	 * 是否启用刷新
	 * 
	 * @return
	 */
	protected boolean isRefreshEnable() {
		return false;
	}

	/**
	 * 是否加载更多
	 * 
	 * @return
	 */
	protected boolean isLoadMoreEnable() {
		return true;
	}
	
	/**
	 *  第一次 加载数据  或者下拉刷新数据
	 */
	protected void onRefreshData() {
		  new Dumpling_SinoAsyncTask<Object, Void, Dumpling_MyCashDumplingBean>(this, false,true) {

	            @Override
	            public Dumpling_MyCashDumplingBean before(Object... params) throws Exception {
	            	Dumpling_BaseVo.initPageData();
	                return Dumpling_RemoteImpl.getInstance().queryMyCashdumpling(Dumpling_SinoConstans.USER_ID, Dumpling_BaseVo.page , Dumpling_BaseVo.pagesize);
	            }

	            @Override
	            public void after(Dumpling_MyCashDumplingBean data) {
	            	
	            	if (!isRefreshed) {
						onRefreshFinish();
					}
	            	
	            	if (data != null && data.resultList != null &&  data.resultList.size() > 0 ) {
	            		showView(hasDataView);
	            		hideViewForGone(noDataView ,loadDataErrorView , hasNetworkConnectedView);
	            		dumpling_MakeCardAdapger.setData(data.resultList);
					} else {
						hideViewForGone(hasDataView , loadDataErrorView , hasNetworkConnectedView);
						showView(noDataView);
					}
	            	
	            	
	            }

	            @Override
	            public void exception() {
	            	onLoadDataError();
	            }
	        }.execute();
	}
	
	/**
	 *  下拉刷新完成
	 */
	private void onRefreshFinish() {
		lv_my_card_dumpling_list.onRefreshComplete();
		isRefreshed = true ;
	}

	/**
	 * 加载数据失败
	 */
	protected void onLoadDataError() {
		if (Dumpling_NetWorkUtil.getNetWork(Dumpling_MakeCardDumplingListActivity.this)) {  // 没有网络
			hideViewForGone(hasDataView , noDataView ,loadDataErrorView );
            showView(hasNetworkConnectedView);
		} else { //  服务器异常
			hideViewForGone(hasDataView , noDataView , hasNetworkConnectedView);
			showView(loadDataErrorView);
		}
	}
	
	/**
	 *  加载更多数据  分页
	 */
	protected void onLoadMoreData() {
		  new Dumpling_SinoAsyncTask<Object, Void, Dumpling_MyCashDumplingBean>(this, false, false) {

	            @Override
	            public Dumpling_MyCashDumplingBean before(Object... params) throws Exception {
	                return Dumpling_RemoteImpl.getInstance().queryMyCashdumpling(98633 + "", Dumpling_BaseVo.nextPage() , Dumpling_BaseVo.pagesize);
	            }

	            @Override
	            public void after(Dumpling_MyCashDumplingBean data) {
	            	
	            		if (data == null || data.resultList == null || data.resultList.size() == 0 || data.getCode() != Dumpling_SinoConstans.SUCCESS_CODE) {
	            			Dumpling_BaseVo.setLoadMoreData(false);
	            			
	            			mHandler.obtainMessage(LOAD_MORE_DATA_ERROR).sendToTarget() ;
	            			
	            			return;
	            		}
	            		
	            		dumpling_MakeCardAdapger.setDataList(data.resultList);
	            		mHandler.obtainMessage(LOAD_MORE_DATA_SUCCESS).sendToTarget() ;
	            }

	            @Override
	            public void exception() {
	            		mHandler.obtainMessage(LOAD_MORE_DATA_ERROR).sendToTarget() ;
	            }
	        }.execute();
	}

	


	
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Object item = parent.getAdapter().getItem(position);
		if (item != null && item instanceof MyCashDumpling) {
			MyCashDumpling myCashDumpling	= (MyCashDumpling)item;
			Bundle bundle = new Bundle();
			bundle.putString("dumplingUserPutmoneytid", myCashDumpling.dumplingUserPutmoneytid);
			goIntent(Dumpling_MyCashDumplingDetailActivity.class, bundle);
			Toast.makeText(this, position +" ===" +myCashDumpling.dumplingUserPutmoneytid , Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	public void onAntiViolence(View view) {

	}
}
